package org.dreamwork.compilation;

import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-2-2
 * Time: 11:03:40
 */
public class VirtualClass {
    private int modifier;
    private String name;
    private String packageName = "";
    private String superClassName;
    private List<String> interfaces = new ArrayList<String> ();
    private List<VirtualField> fields = new ArrayList<VirtualField> ();
    private List<VirtualMethod> methods = new ArrayList<VirtualMethod> ();
    private List<String> imports = new ArrayList<String> ();
    private List<VirtualConstruction> constructions = new ArrayList<VirtualConstruction> ();
    private List<String> codeSnip = new ArrayList<String> ();

    public int getModifier () {
        return modifier;
    }

    public void setModifier (int modifier) {
        this.modifier = modifier;
    }

    public String getName () {
        return name;
    }

    public void setName (String name) {
        this.name = name;
    }

    public String getPackageName () {
        return packageName;
    }

    public void setPackageName (String packageName) {
        this.packageName = packageName;
    }

    public String getSuperClassName () {
        return superClassName;
    }

    public void setSuperClassName (String superClassName) {
        this.superClassName = superClassName;
    }

    public void addField (VirtualField field) {
        if (!fields.contains (field)) fields.add (field);
    }

    public void addMethod (VirtualMethod method) {
        if (!methods.contains (method)) methods.add (method);
    }

    public void addImport (String importItem) {
        if (!imports.contains (importItem)) imports.add (importItem);
    }

    public void addInterface (String interfaceName) {
        if (!interfaces.contains (interfaceName)) interfaces.add (interfaceName);
    }

    public void addDefaultConstruction () {
        VirtualConstruction vc = new VirtualConstruction ();
        vc.setName (name);
        constructions.add (vc);
    }

    public void addConstruction (VirtualField[] vfs) {
        VirtualConstruction vc = new VirtualConstruction ();
        vc.setName (name);

        if (fields != null) for (VirtualField vf : vfs) {
            VirtualParameter vp = new VirtualParameter ();
            vp.setName (vf.getName ());
            vp.setType (vf.getType ());
            vc.addParameter (vp);
            vc.addStatement ("this." + vp.getName () + " = " + vp.getName ());
        }
        constructions.add (vc);
    }

    public void addCodeSnip (String code) {
        codeSnip.add (code);
    }

    public String toString () {
        StringBuffer buff = new StringBuffer ();
        buff.append ("package ").append (packageName).append (";\n");
        for (Object anImport : imports)
            buff.append ("import ").append (anImport).append (";\n");
        buff.append ("\n");
        buff.append (Modifier.experssion [modifier]).append (" class ").append (name).append (" ");
        if (superClassName != null)
            buff.append ("extends ").append (superClassName).append (" ");
        if (interfaces.size () > 0) {
            buff.append ("implements ");
            for (int i = 0; i < interfaces.size (); i ++) {
                if (i != 0) buff.append (", ");
                buff.append (interfaces.get (i));
            }
        }
        buff.append ("{\n");
        for (VirtualField field : fields)
            buff.append (field).append (";\n");
        for (VirtualConstruction construction : constructions)
            buff.append (construction);
        for (String aCodeSnip : codeSnip)
            buff.append (aCodeSnip).append ("\n");
        for (Object method : methods)
            buff.append (method).append ("\n");
        buff.append ("}");
        return buff.toString ();
    }
}